package com.work.servlet.teacher;

import com.work.bean.Middle;
import com.work.service.MiddleService;
import com.work.service.impl.MiddleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Administrator on 2017/10/10.
 */
@WebServlet("/grade")
public class GradeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();

        if (session.getAttribute("isLogin") != null && (Boolean) session.getAttribute("isLogin")) {

            if (req.getParameter("grade") != null){
                int grade = Integer.parseInt(req.getParameter("grade"));
                int studentId = Integer.parseInt(req.getParameter("studentId"));

                MiddleService middleService = new MiddleServiceImpl();

                middleService.updateGrade(new Middle(studentId, Integer.parseInt(String.valueOf(session.getAttribute("workId"))), grade));

                resp.setCharacterEncoding("utf-8");
                resp.getWriter().write("<script>alert('操作成功');window.location='/finish?workId="+session.getAttribute("workId")+"&isFinish=1'</script>");
            }


        }else {
            session.setAttribute("error", "请登录后访问");
            resp.sendRedirect("login.jsp");
        }
    }
}
